/*
 * OfferDialog.java
 * business
 *
 * Created by ChenTao on 2017/5/26.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.utils.RegexUtils;
import com.yidiandao.business.utils.UIUtils;

/**
 * 发布报价 - Dialog
 * <p>
 * Created by ChenTao(chentao7v@gmail.com) on 2017-05-26 14:50
 */
public class OfferDialog extends Dialog {

    private EditText mEtPrice;
    private EditText mEtPhone;
    private TextView mTvCancel;
    private TextView mTvConfirm;
    private EditText mEtShippingFee;

    private TextView mTvUnitGoods;
    private TextView mTvUnitLogistics;

    private OnConfirmOfferInfoListener mListener;

    public OfferDialog(@NonNull Context context) {
        super(context, R.style.ThemeNoTitleDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_offer);

        initViews();
        initListeners();
    }

    private void initViews() {
        mEtPrice = (EditText) findViewById(R.id.et_price);
        mEtShippingFee = (EditText) findViewById(R.id.et_shipping_fee);
        mEtPhone = (EditText) findViewById(R.id.et_phone);
        mTvCancel = (TextView) findViewById(R.id.tv_cancel);
        mTvConfirm = (TextView) findViewById(R.id.tv_confirm);
        mTvUnitGoods = (TextView) findViewById(R.id.tv_unit);
        mTvUnitLogistics = (TextView) findViewById(R.id.tv_unit_logistics);
    }

    private void initListeners() {
        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        mTvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String price = mEtPrice.getText().toString().trim();
                if (TextUtils.isEmpty(price)) {
                    UIUtils.showToast(UIUtils.getString(R.string.input_price_first));
                    return;
                }

                String shippingFee = mEtShippingFee.getText().toString().trim();
                if (TextUtils.isEmpty(shippingFee)) {
                    UIUtils.showToast(UIUtils.getString(R.string.input_shipping_fee_first));
                    return;
                }

                String phone = mEtPhone.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    UIUtils.showToast(UIUtils.getString(R.string.input_phone_first));
                    return;
                }

                if (!(RegexUtils.isPhoneNumber(phone) || RegexUtils.isTelephone(phone))) {
                    UIUtils.showToast(UIUtils.getString(R.string.wrong_phone_number));
                    return;
                }

                dismiss();
                if (mListener != null) {
                    mListener.onConfirmOffer(price, shippingFee, phone);
                }
            }
        });
    }

    public void setOnConfirmOfferInfoListener(OnConfirmOfferInfoListener listener) {
        mListener = listener;
    }

    public void setUnit(String unit) {
        mTvUnitGoods.setText(UIUtils.getString(R.string.offer_unit, unit));
        mTvUnitLogistics.setText(UIUtils.getString(R.string.offer_unit, unit));
    }

    public interface OnConfirmOfferInfoListener {
        void onConfirmOffer(String price, String shippingFee, String phone);
    }
}
